package com.xiaoshuidi.cloud.module.contract.job;

import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.contract.controller.admin.bill.vo.OrderFeesDetaillVo;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.OrderFeesDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.bill.RentOrderDO;
import com.xiaoshuidi.cloud.module.contract.dal.dataobject.contract.OrderLateFeeVo;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.bill.OrderFeesMapper;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.contract.ContractRoomMapper;
import com.xiaoshuidi.cloud.module.contract.service.contract.OrderLateFeeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: lease-module-contract
 * @BelongsPackage: com.xiaoshuidi.cloud.module.contract.job
 * @Author: James
 * @CreateTime: 2023-11-15  11:35
 * @Description: 滞纳金定时任务
 * @Version: 1.0
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ContractFixedFeeJob {
    @Resource
    private OrderLateFeeService orderLateFeeService;
    @Resource
    private ContractRoomMapper contractRoomMapper;
    @Resource
    private OrderFeesMapper orderFeesMapper;
    @XxlJob("contractFixedFeeJob")
    @TenantIgnore
    public void execute() {
        log.info("滞纳金定时任务 - 执行开始");
        //获取已到期的账单
        List<RentOrderDO> orderOverList = contractRoomMapper.getOrderOverList();
        log.info("已到期的账单id:"+ JSONObject.toJSONString(orderOverList.stream()
                .map(item -> item.getId())
                .collect(Collectors.toList())));
        for(RentOrderDO rentOrderDO:orderOverList){
            //判断是否配置滞纳金
            OrderLateFeeVo orderLateFee = orderLateFeeService.getIsOrderFee(rentOrderDO.getApartmentId());
            log.info("滞纳金配置信息："+JSONObject.toJSONString(orderLateFee));
            if(null!=orderLateFee) {
                //判断该账单是否存在滞纳金
                OrderFeesDetaillVo isOrderLateFee = orderFeesMapper.getIsOrderLateFee(rentOrderDO.getId(),17);
                log.info("滞纳金账单费项信息："+JSONObject.toJSONString(isOrderLateFee));
                //判断逾期天数
                long days = ChronoUnit.DAYS.between(rentOrderDO.getStartTime(), LocalDate.now());
                log.info("逾期天数："+days);
                if (null == isOrderLateFee) {
                    //根据配置计算滞纳金额
                    OrderFeesDetaillVo isOrderLateFee1 = orderFeesMapper.getIsOrderLateFee(rentOrderDO.getId(), 1);
                    log.info("租金账单费项信息："+JSONObject.toJSONString(isOrderLateFee1));
                    BigDecimal lateAmount=(isOrderLateFee1.getAmount().multiply((BigDecimal.valueOf(orderLateFee.getPropoetion()).divide(BigDecimal.valueOf(1000))))).multiply(BigDecimal.valueOf(days)).setScale(2, BigDecimal.ROUND_HALF_UP);
                    OrderFeesDO orderFees = new OrderFeesDO();
                    orderFees.setOrderType("default");
                    orderFees.setOrderId(rentOrderDO.getId());
                    orderFees.setFeeSort("once");
                    orderFees.setFeeId(Long.valueOf(17));
                    orderFees.setFeeName("滞纳金");
                    orderFees.setFlowType(0);
                    orderFees.setPayStatus("unpaid");
                    orderFees.setAmount(lateAmount);
                    orderFees.setStartTime(rentOrderDO.getStartTime());
                    orderFees.setEndTime(LocalDate.now());
                    orderFees.setDeptId(rentOrderDO.getDeptId());
                    orderFees.setTenantId(rentOrderDO.getTenantId());
                    orderFees.setOldRentorderAmount(rentOrderDO.getTotalAmount());
                    //插入费项
                    log.info("插入滞纳金账单费项信息："+JSONObject.toJSONString(orderFees));
                    int insert = orderFeesMapper.insert(orderFees);
                    //修改账单金额
                    if(insert==1){
                        contractRoomMapper.updateOrderAmount(rentOrderDO.getTotalAmount().add(lateAmount).setScale(2, RoundingMode.HALF_UP),rentOrderDO.getId());
                    }
                }else{
                    //更新滞纳金
                    BigDecimal lateAmount=(isOrderLateFee.getAmount().multiply((BigDecimal.valueOf(orderLateFee.getPropoetion()).divide(BigDecimal.valueOf(1000))))).multiply(BigDecimal.valueOf(days)).setScale(2, RoundingMode.HALF_UP);
                    int i = orderFeesMapper.updateOrderLateFeeAmount(lateAmount, isOrderLateFee.getId());
                    if(i==1){
                        //更新账单总金额
                        if(null!=isOrderLateFee.getOldRentorderAmount()) {
                            contractRoomMapper.updateOrderAmount(isOrderLateFee.getOldRentorderAmount().add(lateAmount).setScale(2, RoundingMode.HALF_UP), rentOrderDO.getId());

                        }
                    }

                }
            }

        }

    }
}

















